package com.spica.spicaschool.ui.mydiary

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.drakeet.multitype.ItemViewDelegate
import com.spica.spicaschool.R
import com.spica.spicaschool.bean.diary.Diary
import com.spica.spicaschool.databinding.ItemMyDiaryBinding
import com.spica.spicaschool.utils.toUid
import org.ocpsoft.prettytime.PrettyTime
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.*

/**
 * 我的日志动态列表
 */
class MyDiaryItemDelegate constructor(private val activity: Activity) :
    ItemViewDelegate<Diary, MyDiaryItemDelegate.ViewHolder>() {

    private val prettyTime = PrettyTime(Locale.CHINA)

    override fun getItemId(item: Diary): Long = item.diaryUid.toUid().toLong()

    // 视图绑定
    class ViewHolder(val viewBinding: ItemMyDiaryBinding) :
        RecyclerView.ViewHolder(viewBinding.root)

    private val navController =
        Navigation.findNavController(activity, R.id.fragment_container_view)


    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup
    ): ViewHolder {
        val viewBinding = ItemMyDiaryBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(viewBinding)
    }

    @SuppressLint("BinaryOperationInTimber")
    override fun onBindViewHolder(holder: ViewHolder, item: Diary) {
        val isLike = item.isLike


        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        val date = sdf.parse(item.diaryTime)

        // UI填充
        with(holder.viewBinding) {
            tvInfo.text = item.diaryContentPreview
            tvPushTime.text = prettyTime.format(date)
            tvTitle.text = item.diaryTitle
            tvLikeNum.text = "${item.diaryLikeNum}"
            tvReplyNum.text = "${item.diaryCommentNum}"
            if (isLike == 1) btnLike.load(R.drawable.ic_like_selected)
            // 点击查看详情
            root.setOnClickListener {
                val bundle = bundleOf(Pair("DiaryId", item.diaryId))
                Timber.e("XX" + navController.currentDestination?.navigatorName)
                navController.navigate(R.id.action_home_to_diary_see, bundle)
            }
            btnLike.setOnClickListener {
                when (isLike) {
                    0 -> {
                        holder.viewBinding.btnLike.load(R.drawable.ic_like_selected) {
                            size(
                                holder.viewBinding.btnLike.width,
                                holder.viewBinding.btnLike.height
                            )
                        }
                    }
                    else -> {
                        // unlikeAnim.show()
                        holder.viewBinding.btnLike.load(R.drawable.ic_like_normal) {
                            size(
                                holder.viewBinding.btnLike.width,
                                holder.viewBinding.btnLike.height
                            )
                        }
                    }
                }
            }
        }
    }
}
